package com.carlinfo.hospital.dao.impl;

import com.carlinfo.hospital.dao.Doctor_personDao;
import com.carlinfo.hospital.pojo.Doctor_person;
import com.carlinfo.hospital.pojo.Order;
import com.carlinfo.hospital.util.DataSourceUtil;
import com.carlinfo.hospital.util.PageInfoUtil;
import com.carlinfo.hospital.util.ReflectUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Repository
public class Doctor_personDaoImpl implements Doctor_personDao
{
    QueryRunner queryRunner = new QueryRunner(DataSourceUtil.getInstance());
    private String sql = "";

    @Override
    public int saveOneDao(Doctor_person doctor_person)
    {
        sql = "insert into doctor_person values(null,?,?,?,?,?,?)";
        try
        {
            Object[] objects = ReflectUtil.zh(doctor_person);
            int i = queryRunner.execute(sql, objects);
            return i;
        } catch (SQLException e)
        {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Doctor_person> selectListDao(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
    {
        sql = "select * from doctor_person where 1=1 ";
        List<Object> list = new ArrayList<Object>();
        if (condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
        {
            sql += " and name like ? ";
            list.add("%" + condMap.get("keyword") + "%");
        }
        if (condMap.get("status") != null && !"".equalsIgnoreCase(condMap.get("status") + ""))
        {
            sql += " and status like ? ";
            list.add("%" + condMap.get("status") + "%");
        }
        if (condMap.get("stDate") != null && !"".equalsIgnoreCase(condMap.get("edDate") + ""))
        {
            sql += " and (publicTime>? and publicTime<?)";
            list.add(condMap.get("stDate"));
        }
        if (condMap.get("doctorId") != null && !"".equalsIgnoreCase(condMap.get("doctorId") + ""))
        {
            sql += " and doctorId = ?";
            list.add(condMap.get("doctorId"));
        }
        System.out.println("sql:" + sql);
        Object[] s = new Object[list.size()];
        System.out.println("list長度：" + list.size());
        list.toArray(s);
        try
        {
            List<Doctor_person> res = queryRunner.query(sql, new BeanListHandler<Doctor_person>(Doctor_person.class), s);
            return res;
        } catch (SQLException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Doctor_person selectOneDao(Map<String, Object> condMap)
    {
        List<Object> list = new ArrayList<Object>();
        if (!"".equalsIgnoreCase(condMap.get("name") + "") && condMap.get("name") != null)
        {
            sql = "select * from doctor_person where name= ? ";
            list.add(condMap.get("name"));
        }
        if (!"".equalsIgnoreCase(condMap.get("id") + "") && condMap.get("id") != null)
        {
            sql = "select * from doctor_person where id= ? ";
            list.add(condMap.get("id"));
        }
        try
        {
            Doctor_person res = queryRunner.query(sql, new BeanHandler<Doctor_person>(Doctor_person.class), list.get(0));
            return res;
        } catch (SQLException e)
        {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public int updateOneDao(Doctor_person doctor_person)
    {
        sql = "update doctor_person set doctorId=?,personId=?,status=?,createTime=?,updateTime=?,publicTime=? where id =?";
        try
        {
            int execute = queryRunner.execute(sql, ReflectUtil.zh(doctor_person));
            return execute;
        } catch (SQLException e)
        {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int deleteOneDao(Map<String, Object> condMap)
    {
        List<Object> list = new ArrayList<Object>();
        if (!"".equalsIgnoreCase(condMap.get("name") + "") && condMap.get("name") != null)
        {
            sql = "delete * from doctor_person where name= ? ";
            list.add(condMap.get("name"));
        }
        if (!"".equalsIgnoreCase(condMap.get("id") + "") && condMap.get("id") != null)
        {
            sql = "delete * from doctor_person where id= ? ";
            list.add(condMap.get("id"));
        }
        String[] strings = new String[list.size()];
        try
        {
            int execute = queryRunner.execute(sql, list.toArray(strings));
            return execute;
        } catch (SQLException e)
        {
            e.printStackTrace();
        }
        return 0;
    }
}
